package com.zijing2333.adaptivescheduling.criticalNodeJudgment;



import android.util.Log;

import com.alibaba.fastjson.JSON;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;


@Deprecated
public class HttpClient {
    public static String sendGet(String url, HashMap<String, String> parameter) {
        Log.d("url", url);
        HttpURLConnection connection;
        BufferedReader buffer;
        StringBuffer result = null;
        try {
            StringBuilder targetUrlBuilder = new StringBuilder(url);
            if (parameter != null) {
                targetUrlBuilder.append("?");
                StringBuilder queryString = new StringBuilder();
                for (Map.Entry<String, String> pair : parameter.entrySet()) {
                    queryString.append(pair.getKey());
                    queryString.append("=");
                    queryString.append(URLEncoder.encode(pair.getValue(), "UTF-8").replace("+", "%20"));
                    queryString.append("&");
                }
                targetUrlBuilder.append(queryString.substring(0, queryString.length() - 1));
            }
            URL urlObj = new URL(targetUrlBuilder.toString());
            Log.d("URL", targetUrlBuilder.toString().replace("http", ""));
            connection = (HttpURLConnection) urlObj.openConnection();
            connection.setRequestMethod("GET");
            connection.setDoOutput(true);
            connection.setDoInput(true);
            connection.setUseCaches(false);
            int responseCode = connection.getResponseCode();
            Log.d("HttpClient", String.format("response code: %d", responseCode));
            if (responseCode == HttpURLConnection.HTTP_OK) {
                InputStream inputStream = connection.getInputStream();
                result = new StringBuffer();
                String line;
                buffer = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
                while ((line = buffer.readLine()) != null) {
                    result.append(line);
                }
            }
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        assert result != null;
        return result.toString();
    }

    public static String sendPost(String url, ArrayList<Object> parameter) {
        StringBuffer responseResult = new StringBuffer();
        try {
            URL urlObj = new URL(url);
            HttpURLConnection connection = (HttpURLConnection) urlObj.openConnection();
            connection.setRequestMethod("POST");
            connection.setRequestProperty("accept","*/*");
            connection.setRequestProperty("connection","Keep-Alive");
            connection.setRequestProperty("Content-Type","application/json");
            connection.setRequestProperty("Accept", "application/json");
            connection.setDoOutput(true);
            connection.setDoInput(true);
            connection.setUseCaches(false);
            OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), StandardCharsets.UTF_8);
            out.append("[" + JSON.toJSONString(parameter) + "]");
//             System.out.println(JSON.toJSONString(parameter));
            out.flush();
            out.close();
            int responseCode = connection.getResponseCode();
            // System.out.println(responseCode);
            if (responseCode == HttpURLConnection.HTTP_OK) {
                BufferedReader reader = new BufferedReader(
                        new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8));
                String line;
                while ((line = reader.readLine()) != null) {
                    responseResult.append(line);
                }
                reader.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return responseResult.toString();
    }


}


